package com.ygg.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ygg.cmn.client.DictFeignClient;
import com.ygg.enums.DictEnum;
import com.ygg.exception.YyghException;
import com.ygg.model.user.Patient;
import com.ygg.result.ResultCodeEnum;
import com.ygg.service.PatientService;
import com.ygg.mapper.PatientMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient>
        implements PatientService {

    @Resource
    DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllUserId(Long userId) {
        List<Patient> patients = this.query().eq("user_id", userId).list();
        //其他参数封装
        patients.stream().forEach(this::packPatient);

        return patients;
    }


    private void packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString = "";

        if (!StringUtils.isEmpty(patient.getCertificatesType())) {
            certificatesTypeString =
                    dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        }

        //联系人证件类型
        String contactsCertificatesTypeString = "";
        if (!StringUtils.isEmpty(patient.getContactsCertificatesType())) {
            contactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        }
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());

        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());

    }

    @Override
    public Patient getPatientId(Long id) {
        Patient patient = this.getById(id);
        this.packPatient(patient);
//        dictFeignClient.getName(patient.)
        return patient;
    }

    @Override
    public void addPatient(Patient patient) {
        boolean is = this.save(patient);
        if (!is) throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }

    @Override
    public void deleteById(Long id) {
        boolean is = this.removeById(id);
        if (!is) throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }

    @Override
    public void updatePatientById(Patient patient) {
        boolean is = this.updateById(patient);
        if (!is) throw new YyghException(ResultCodeEnum.PARAM_ERROR);
    }

    @Override
    public Patient getPatientOrder(Long id) {
        Patient patient = this.getById(id);
        this.packPatient(patient);
        return patient;
    }
}




